/*
 * @(#)cn.design.pattern.command 2020/4/23/0023
 *
 * Copy Right@ 人人译视界
 */


package cn.design.pattern.command;

/**
 * <pre>
 * @author tuwei
 *
 * @version 0.9
 *
 * 修改版本: 0.9
 * 修改日期: 2020/4/23/0023
 * 修改人 :  tuwei
 * 修改说明: 初步完成
 * 复审人 ：
 * </pre>
 */
public class RemoteController {
	Command[] onCommands;
	Command[] offCommands;

	// 执行撤销的命令
	Command undoCommand;

	public RemoteController() {
		this.onCommands = new Command[5];
		this.offCommands = new Command[5];

		for (int i = 0; i < 5; i++) {
			onCommands[i] = new NoCommand();
			offCommands[i] = new NoCommand();
		}
	}

	public void setCommand(int no, Command onCommand, Command offCommand) {
		onCommands[no] = onCommand;
		offCommands[no] = offCommand;
	}

	public void onButtonWasPushed(int no) {
		//找到按下遥控器的开的按钮
		Command onCommand = onCommands[no];
		onCommand.execute();
		// 记录这次的操作，用于撤销
		undoCommand = onCommand;
	}

	public void offButtonWasPushed(int no) {
		//找到按下遥控器的开的按钮
		Command offCommand = offCommands[no];
		offCommand.execute();
		// 记录这次的操作，用于撤销
		undoCommand = offCommand;
	}

	/**
	 * 按下撤销按钮
	 */
	public void undoButtonWasPushed() {
		undoCommand.undo();
	}
}
